from pydantic_settings import BaseSettings
from typing import Optional, List
import os


class Settings(BaseSettings):
    """应用配置"""

    # 基础配置
    ENVIRONMENT: str = "development"
    DEBUG: bool = True
    LOG_LEVEL: str = "INFO"

    # 数据库配置
    DATABASE_URL: str = "postgresql://videoadmin:videoadmin@localhost:5432/video_platform"
    DB_HOST: str = "localhost"
    DB_PORT: int = 5432
    DB_USER: str = "videoadmin"
    DB_PASSWORD: str = "videoadmin"
    DB_NAME: str = "video_platform"

    # Redis配置
    REDIS_URL: str = "redis://:redispass@localhost:6379/0"
    REDIS_HOST: str = "localhost"
    REDIS_PORT: int = 6379
    REDIS_PASSWORD: str = "redispass"
    REDIS_DB: int = 0

    # Celery配置
    CELERY_BROKER_URL: str = "redis://:redispass@localhost:6379/0"
    CELERY_RESULT_BACKEND: str = "redis://:redispass@localhost:6379/1"
    CELERY_TASK_SERIALIZER: str = "json"
    CELERY_ACCEPT_CONTENT: List[str] = ["json"]
    CELERY_RESULT_SERIALIZER: str = "json"
    CELERY_TIMEZONE: str = "UTC"

    # S3存储配置
    S3_ENDPOINT: str = "http://minio:9000"
    S3_ACCESS_KEY: str = "minioadmin"
    S3_SECRET_KEY: str = "minioadmin"
    S3_BUCKET: str = "videos"
    S3_REGION: str = "us-east-1"
    S3_USE_SSL: bool = False
    S3_VERIFY_SSL: bool = False

    # OpenAI配置
    OPENAI_API_KEY: Optional[str] = None
    OPENAI_MODEL: str = "gpt-4"
    WHISPER_MODEL: str = "base"

    # JWT配置
    JWT_SECRET: str = "your-super-secret-jwt-key-change-in-production"
    JWT_ALGORITHM: str = "HS256"
    JWT_EXPIRATION_HOURS: int = 24
    REFRESH_TOKEN_EXPIRATION_DAYS: int = 7

    # CORS配置
    CORS_ORIGINS: List[str] = ["http://localhost:3000", "http://localhost:5173", "http://frontend"]
    CORS_ALLOW_CREDENTIALS: bool = True
    CORS_ALLOW_METHODS: List[str] = ["*"]
    CORS_ALLOW_HEADERS: List[str] = ["*"]

    # 文件处理配置
    MAX_VIDEO_SIZE_MB: int = 5000
    SUPPORTED_VIDEO_FORMATS: List[str] = ["mp4", "avi", "mov", "mkv", "webm"]
    SUPPORTED_AUDIO_FORMATS: List[str] = ["mp3", "wav", "aac", "m4a", "flac"]

    # 视频处理配置
    VIDEO_PROCESSING_TIMEOUT: int = 3600  # 1小时
    TRANSCRIPTION_LANGUAGE: str = "zh"
    MAX_WORKER_CONCURRENCY: int = 4

    # 功能开关
    ENABLE_TRANSCRIPTION: bool = True
    ENABLE_TRANSLATION: bool = True
    ENABLE_SUMMARIZATION: bool = True
    ENABLE_SENTIMENT_ANALYSIS: bool = True
    ENABLE_SUBTITLE_GENERATION: bool = True
    ENABLE_SCENE_DETECTION: bool = True

    # WebSocket配置
    WS_HEARTBEAT_INTERVAL: int = 30
    WS_CONNECTION_TIMEOUT: int = 600

    # 平台模板配置
    DEFAULT_PLATFORM_TEMPLATES: List[str] = [
        "douyin_short",  # 抖音短视频
        "xiaohongshu",   # 小红书
        "bilibili",      # B站
        "weibo_video",   # 微博视频
    ]

    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
        case_sensitive = True


# 创建全局配置实例
settings = Settings()


def get_settings() -> Settings:
    """获取配置实例"""
    return settings